package com.example.appinting.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.appinting.QueueActivity;
import com.example.appinting.R;
import com.example.appinting.SQL.MysqlHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.List;

public class myAppointAdapter extends BaseAdapter {

    private Context context;
    private List<HashMap<String, Object>> dataList;
    private myAppointAdapter.ViewHolder vh;



//    MyFilter mFilter ;

    private int[] pics={R.drawable.logo};
    public myAppointAdapter(Context context, List<HashMap<String, Object>> list) {
        this.context = context;
        this.dataList = list;
    }
    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        System.out.println(dataList.size());
        System.out.println("进入myAppointAdapter");
        vh = new ViewHolder();
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.myappoint_item, null);
            vh.icon = (ImageView) view.findViewById(R.id.icon);
            vh.time = (TextView) view.findViewById(R.id.txtTime);
            vh.name = (TextView) view.findViewById(R.id.txtName);
            vh.department = (TextView) view.findViewById(R.id.department);
            vh.btn_payorcheck = (Button) view.findViewById(R.id.btn_payorcheck);

            view.setTag(vh);
        } else {
            vh = (myAppointAdapter.ViewHolder) view.getTag();
        }


        if (dataList.size() > 0) {
//            Toast.makeText(context,  String.valueOf(dataList.size()), Toast.LENGTH_SHORT).show();
            HashMap<String, Object> map = dataList.get(position);


            vh.name.setText(map.get("Doctor_name").toString());
            vh.department.setText(map.get("Department").toString());
            vh.time.setText(map.get("Time").toString());
            vh.icon.setImageResource((Integer) pics[0]);
            vh.btn_payorcheck.setText(map.get("State").toString());

            vh.btn_payorcheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (map.get("State").toString().equals("查看")){
                        System.out.println("点击查看按钮");
                        Toast.makeText(context, "查看排队", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        System.out.println(map.get("State").toString());
                        Toast.makeText(context, "你已完成缴费", Toast.LENGTH_SHORT).show();
                        upAppointStatus(map.get("Appoint_id").toString());
//                        去排队
                        //获取position;
                        int position = getPosition(map.get("Doctor_id").toString());
                        upQueue(map.get("Doctor_id").toString(),map.get("Patient_id").toString(),position);
                    }
                    Intent intent = new Intent(context, QueueActivity.class);
                    intent.putExtra("doctor_id",map.get("Doctor_id").toString());
                    intent.putExtra("patient_id",map.get("Patient_id").toString());
                    context.startActivity(intent);
                }
            });
        }

        return view;
    }

    class ViewHolder {
        TextView time;
        ImageView icon; TextView name, department;Button btn_payorcheck;
    }


    @Override
    public int getCount() {
        if (dataList != null) {
            return dataList.size(); }
        else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;

    }
    void upAppointStatus(String appoint_id){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            // 连接到 MySQL 数据库
            connection = MysqlHelper.getConnection();
            // 准备 SQL 语句
            System.out.println("upAppointStatus:");
            String sql = "UPDATE reservation SET appoint_state = 1 WHERE id = " + appoint_id;
            statement = connection.prepareStatement(sql);

            // 执行 SQL 语句
            int x = statement.executeUpdate();
            // 处理查询结果
            if(x == 1){
                System.out.println("更新成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接和资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    void upQueue(String doctor_id,String patient_id,int position){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            // 连接到 MySQL 数据库
            connection = MysqlHelper.getConnection();
            // 准备 SQL 语句
            System.out.println("upQueue:");
            String sql = "INSERT INTO queue (doctor_id, patient_id, position) VALUES ( " +
                    doctor_id + ", " + patient_id + ", " + position +")";
            statement = connection.prepareStatement(sql);

            // 执行 SQL 语句
            int x = statement.executeUpdate();
            // 处理查询结果
            if(x == 1){
                System.out.println("queue更新成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接和资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    int getPosition (String doctor_id){
        Connection connection = null;
        PreparedStatement statement = null;

        ResultSet resultSet = null;

        try {
            // 连接到 MySQL 数据库
            connection = MysqlHelper.getConnection();
            // 准备 SQL 语句
            String sql = "SELECT COUNT(*) FROM queue WHERE doctor_id = " + doctor_id;
            statement = connection.prepareStatement(sql);

            // 执行 SQL 语句
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                int position = resultSet.getInt(1);
                // 处理查询结果
                return position;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接和资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return 0;
    }
}
